﻿using AutoMapper;
using BW.MesProject.Public.ErrorCode;
using BW.MesProject.Public.Infrastructure;
using BW.MesProject.WareHouse.Domain;
using BW.MesProject.WareHouse.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BW.MesProject.WareHouse.Application
{

    /// <summary>
    /// 基础信息
    /// </summary>
    public class BaseServices : IBaseServices
    {
        private readonly IBaseRepository<TypeCodeModel> _Code;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="mapper"></param>
        public BaseServices(IBaseRepository<TypeCodeModel> Code, IMapper mapper)
        {
            _Code = Code;
            this._mapper = mapper;
        }


        /// <summary>
        /// 查询编码
        /// </summary>
        /// <param name="Type"></param>
        /// <returns></returns>
        public ApiResult<TypeCodeModel> GetCode(string Type)
        {
            try
            {
                int totalCount = 0;
                //查询名称来显示数据
                var list = _Code.GetValue(x => x.CodeType == Type);
                return new ApiResult<TypeCodeModel>()
                {
                    Code = ApiEnums.Success,
                    Message = "查询成功",
                    Data = list,
                    TotalCount = totalCount,
                    PageNum = 0,
                    PageSize = 0
                };
            }
            catch (Exception ex)
            {
                Console.WriteLine($"查询编码信息时发生异常: {ex.Message}");
                return new ApiResult<TypeCodeModel>()
                {
                    Code = ApiEnums.Fail,
                    Message = $"查询失败: {ex.Message}",
                    Data = new TypeCodeModel(),
                    TotalCount = 0,
                    PageNum = 0,
                    PageSize = 0
                };
            }

        }

        /// <summary>
        /// 修改编码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateCode(TypeCodeModel model)
        {
            var Data = _Code.Update(model);
            return new ApiResult<int>()
            {
                Code = ApiEnums.Success,
                Message = "修改成功",
                Data = Data,
                TotalCount = 0
            };
        }
    }
}
